<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.js"></script>
</head>
<body>
  <script>
    /* new Promise((resolve, reject) => { // 执行器   执行....的函数
        // 执行异步任务
        setTimeout(() => {
          // 如果成功了, 将promise变为成功的
          // resolve(1)   // value  在axios中称为response
          // 如果失败了, 将promise变为失败的
          // reject(2)  // reason  在axios中称为error
          throw 3
        }, 1000);
    }) */



    
    /* 
    then返回的promise的结果状态由 then指定的回调函数执行的结果决定
    1. 抛出错误   => 失败, reason就是抛出的错误
    2. 返回失败的promise => 失败, reason是失败promise的reason
    3. 返回成功的promise => 成功, value是成功promise的value
    4. 返回pending的promise => 还是peding
    5. 返回其它任意值 => 成功, value就是返回值
    */


   /*  new Promise((resolve, reject) => { // 执行器   执行....的函数
      reject(1)
    })
    .then(
      value => {
        console.log('onResolved1', value)
      },
      reason => {
        console.log('onRejected1', reason)

        // 1. 抛出错误   => 失败, reason就是抛出的错误
        // throw 3
        // 2. 返回失败的promise => 失败, reason是失败promise的reason
        // return Promise.reject(2)
        // 3. 返回成功的promise => 成功, value是成功promise的value
        // return Promise.resolve(4)
        // 4. 返回pending的promise => 还是peding
        // return new Promise(() => {})
        // 5. 返回其它任意值 => 成功, value就是返回值
        // return 5
        

      }
    )
    .then(
      value => {
        console.log('onResolved2', value)
        // throw 6
        return 7
      },
      reason => {
        console.log('onRejected2', reason)
      }
    )
    .catch(  // 本质: then(value => value, onRejected)
      reason => {
        console.log('catch cb', reason)
      }
    )
    .then(
      value => {
        console.log('onResolved3', value)
        // throw 8
        return new Promise(() => {})
      },
      reason => {
        console.log('onRejected3', reason)
      }
    )
    .finally(() => {   // 返回的promise由调用finally的promise决定
      console.log('finally cb')
    })
    .catch(reason => {
      console.log('catch2 cb')
    }) */

    /* const p1 = Promise.reject(3)
    const p2 = Promise.resolve(4)
    const p3 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(5)
      }, 1000);
    })
    const p4 = Promise.all([p3, p2, p1])
    console.log(p4)
    const p5 = Promise.allSettled([p3, p1, p2])
    console.log(p5)
    const p6 = Promise.race([p3, p1, p2])
    console.log(p6) */


    async function fn() {
      try {
        const response = await axios.get('./08_product2.json')
        return response.data
      } catch (error) {
        console.log(error)
        throw error
        // return Promise.reject(error)
      }
    }

    function test() {
      const p = fn()
      console.log(p)
    }
    test()


    /* 
    回调函数:
    1. 你定义
    2. 你不去亲自调用
    3. 在某个时刻它最终会自动执行
    */

  </script>
</body>
</html>